/*
 * @lc app=leetcode.cn id=824 lang=java
 *
 * [824] 山羊拉丁文
 */

// @lc code=start
class Solution
{
  static final Set<Character> VOWELS = new HashSet<Character>() {{
    add('a');
    add('e');
    add('i');
    add('o');
    add('u');
    add('A');
    add('E');
    add('I');
    add('O');
    add('U');
  } };

  public String toGoatLatin(String sentence)
  {
    String[] words = sentence.split(" ");
    int n = words.length;
    StringBuilder[] sbList = new StringBuilder[n];
    for (int i = 0; i < n; ++i) {
      StringBuilder tmp = new StringBuilder(words[i]);
      if (!VOWELS.contains(tmp.charAt(0))) {
        char front = tmp.charAt(0);
        tmp.append(front);
        tmp.deleteCharAt(0);
      }
      tmp.append("ma");
      for (int j = 0; j <= i; ++j)
        tmp.append('a');
      tmp.append(' ');
      sbList[i] = tmp;
    }

    StringBuilder sb = new StringBuilder();
    for (StringBuilder w : sbList)
      sb.append(w);
    sb.deleteCharAt(sb.length() - 1);
    return sb.toString();
  }
}
// @lc code=end
